'use strict'

let fs = require('fs');
let path = require('path');
let router = require('koa-router')();

function blue() {
    let files = fs.readdirSync(__dirname);
    let black = files.filter((name) => {
        return name.endsWith('.js') && name !== 'index.js';
    })
    return black;
}

function green(files) {
    files.forEach(item => {
        let temFiles = path.join(__dirname, item);
        let temRoutes = require(temFiles);

        for (let ch in temRoutes) {
            let type = temRoutes[ch][0];
            let fn = temRoutes[ch][1];

            if (type === 'get') {
                router.get(ch, fn);
            } else if (type === 'post') {
                router.post(ch, fn);
            } else if (type === 'put') {
                router.put(ch, fn);
            } else if (type === 'delete') {
                router.delete(ch, fn);
            }
        }
    });
}

module.exports = function () {
    let res = blue();
    green(res);
    return router.routes();
}